package io.github.sham2k.validation.config.bean;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 校验规则所属参数配置
 */
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class ParameterDefine {
    @JacksonXmlProperty(localName = "name", isAttribute = true)
    private String name;

    @JacksonXmlProperty(localName = "value")
    @JacksonXmlElementWrapper(localName = "values", useWrapping = false)
    private List<String> values;

    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @JacksonXmlText
    private String directValue;

    public List<String> getValues() {
        if (values == null || values.isEmpty()) {
            if (directValue != null) {
                if (values == null) {
                    values = new ArrayList<>(1);
                }
                values.add(directValue);
            }
        }
        return values;
    }
}
